package container

import (
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/application/service"
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/session/repository"
	sessionService "sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/session/service"
	userRepository "sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/user/repository"
	userService "sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/domain/user/service"
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/interfaces/handler"
	"sen-golang-study/go_game_study/hero_story_go_server/game_server/internal/interfaces/websocket"
)

// Container 依赖注入容器
type Container struct {
	// 仓储层
	userRepo    userRepository.UserRepository
	sessionRepo repository.SessionRepository

	// 领域服务层
	userDomainService    *userService.UserDomainService
	sessionDomainService *sessionService.SessionDomainService

	// 应用服务层
	userLoginService         *service.UserLoginService
	sessionManagementService *service.SessionManagementService

	// 接口层
	cmdHandlerFactory *handler.CmdHandlerFactory
	webSocketHandler  *websocket.WebSocketHandler
	userLoginHandler  *handler.UserLoginHandler
}

// NewContainer 创建依赖注入容器
func NewContainer() *Container {
	c := &Container{}
	c.initRepositories()
	c.initDomainServices()
	c.initApplicationServices()
	c.initInterfaceLayer()
	c.registerHandlers()
	return c
}

// initRepositories 初始化仓储层
func (c *Container) initRepositories() {
	c.userRepo = userRepository.NewUserRepository()
	c.sessionRepo = repository.NewSessionRepository()
}

// initDomainServices 初始化领域服务层
func (c *Container) initDomainServices() {
	c.userDomainService = userService.NewUserDomainService(c.userRepo)
	c.sessionDomainService = sessionService.NewSessionDomainService(c.sessionRepo)
}

// initApplicationServices 初始化应用服务层
func (c *Container) initApplicationServices() {
	c.userLoginService = service.NewUserLoginService(c.userDomainService)
	c.sessionManagementService = service.NewSessionManagementService(c.sessionDomainService)
}

// initInterfaceLayer 初始化接口层
func (c *Container) initInterfaceLayer() {
	c.cmdHandlerFactory = handler.NewCmdHandlerFactory()
	c.webSocketHandler = websocket.NewWebSocketHandler(c.cmdHandlerFactory, c.sessionManagementService)
	c.userLoginHandler = handler.NewUserLoginHandler(c.userLoginService, c.sessionManagementService)
}

// registerHandlers 注册命令处理器
func (c *Container) registerHandlers() {
	// 注册用户登录处理器
	c.cmdHandlerFactory.RegisterHandler(10100, c.userLoginHandler.HandleUserLogin) // 10100 为用户登录命令码
}

// GetWebSocketHandler 获取WebSocket处理器
func (c *Container) GetWebSocketHandler() *websocket.WebSocketHandler {
	return c.webSocketHandler
}

// GetCmdHandlerFactory 获取命令处理器工厂
func (c *Container) GetCmdHandlerFactory() *handler.CmdHandlerFactory {
	return c.cmdHandlerFactory
}

// GetUserLoginService 获取用户登录服务
func (c *Container) GetUserLoginService() *service.UserLoginService {
	return c.userLoginService
}

// GetSessionManagementService 获取会话管理服务
func (c *Container) GetSessionManagementService() *service.SessionManagementService {
	return c.sessionManagementService
}

// GetUserDomainService 获取用户领域服务
func (c *Container) GetUserDomainService() *userService.UserDomainService {
	return c.userDomainService
}

// GetSessionDomainService 获取会话领域服务
func (c *Container) GetSessionDomainService() *sessionService.SessionDomainService {
	return c.sessionDomainService
}

// GetUserRepository 获取用户仓储
func (c *Container) GetUserRepository() userRepository.UserRepository {
	return c.userRepo
}

// GetSessionRepository 获取会话仓储
func (c *Container) GetSessionRepository() repository.SessionRepository {
	return c.sessionRepo
}